package com.oil.petro.eshop.dao;

import com.oil.petro.eshop.entity.Category;
import com.oil.petro.eshop.entity.Manufacturer;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Property;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("ManufacturerDAO")
public class ManufacturerDAOImpl implements ManufacturerDAO {

    @Autowired
    private SessionFactory sessionfactory;

    @Override
    public Manufacturer get(int id) {
        return (Manufacturer) sessionfactory.getCurrentSession().get(Manufacturer.class, id);

    }

    @Override
    public void delete(Manufacturer manufacturer) {
        sessionfactory.getCurrentSession().delete(manufacturer);
    }

    @Override
    public int save(Manufacturer manufacturer) {
        return (int) sessionfactory.getCurrentSession().save(manufacturer);
    }

    @Override
    public void update(Manufacturer manufacturer) {
        sessionfactory.getCurrentSession().update(manufacturer);
    }

    @Override
    public List<Manufacturer> getList() {
        return sessionfactory.getCurrentSession().createCriteria(Manufacturer.class)
                .addOrder(Property.forName("name").asc())
                .list();
    }

     public int getId(String name){
      Query query =  sessionfactory.getCurrentSession().createQuery("from Manufacturer WHERE name='" + name + "'");
       System.out.println("ssssm "+query.getQueryString());
       Manufacturer manufacturer = (Manufacturer) query.uniqueResult();
       
      
       return manufacturer.getId();
        
    }
    
    
}
